package cn.wecloud.im.multiplayervideodemo.act

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import cn.wecloud.im.WeCloud
import cn.wecloud.im.callback.IMClientCallback
import cn.wecloud.im.core.im.IMClient
import cn.wecloud.im.exception.IMException
import cn.wecloud.im.multiplayervideodemo.App
import cn.wecloud.im.multiplayervideodemo.R
import cn.wecloud.im.multiplayervideodemo.http.HttpManager
import cn.wecloud.im.multiplayervideodemo.http.SchedulerUtils
import cn.wecloud.im.multiplayervideodemo.http.errorConsumer
import cn.wecloud.im.multiplayervideodemo.utils.AppSharePre
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : AppCompatActivity() {
    private val TAG = "LoginActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        title = "登录"
        setContentView(R.layout.activity_login)


        btnLogin.setOnClickListener {
            val clientId = etClientId.text.toString().trim()
            if (clientId.isEmpty()) {
                Toast.makeText(this, "请输入ClientId", Toast.LENGTH_LONG).show()
                return@setOnClickListener
            }
            loginSdk(clientId)
        }

        if (IMClient.getInstance().isLogin && !TextUtils.isEmpty(AppSharePre.getClientId())) {
            Log.e("TAG","isLogin:${IMClient.getInstance().isLogin},ClientId:${AppSharePre.getClientId()}")
            IMClient.getInstance().open(object : IMClientCallback() {
                override fun done(client: IMClient?, e: IMException?) {
                    if (e != null) {
                        runOnUiThread {
                            Toast.makeText(
                                this@LoginActivity,
                                e.toString(),
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    } else {
                        val intent = Intent(this@LoginActivity, MainActivity::class.java)
                        startActivity(intent)
                        finish()
                    }
                }
            })
            return
        }
    }

    private fun loginSdk(clientId: String) {
        val timestamp = "${System.currentTimeMillis()}"
        val d2 = HttpManager().getSign(
            clientId, App.APP_KEY, App.APP_SECRET, timestamp, 2
        ).compose(SchedulerUtils.ioToMain())
            .subscribe({
                val sign = it
                Log.e("LoginActivity", "sign: $sign")
                IMClient.getInstance().login(clientId, App.APP_KEY, timestamp, sign, 2,
                    object : IMClientCallback() {
                        override fun done(client: IMClient?, e: IMException?) {
                            if (e != null) {
                                runOnUiThread {
                                    Toast.makeText(
                                        this@LoginActivity,
                                        e.toString(),
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                            } else {
                                AppSharePre.setToken(WeCloud.getToken())
                                AppSharePre.setClientId(clientId)
                                startActivity(
                                    Intent(
                                        this@LoginActivity, MainActivity::class.java
                                    )
                                )
                                finish()
                            }
                        }

                    })
            }, errorConsumer { msg ->
                Toast.makeText(this@LoginActivity, msg, Toast.LENGTH_SHORT).show()
            })
    }
}